﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace AruaRose
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public static MainWindow Instance { get; private set; }
        private bool _gameActive = false;

        private MainWindow()
        {
            InitializeComponent();
        }
        static MainWindow()
        {
            Instance = new MainWindow();
        }

        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            try
            {
                Functions.LogWrite("Spawning threads.");
                Functions.LogWrite("Going into loop until I find the game process.");
                CClass.Threading.RunAsThread(CheckForProcess);
                CClass.Threading.RunAsThread(FillCharachterClass);
            }
            catch (Exception x)
            {
                Functions.LogWrite(x.Message);
            }
        }
        private void CheckForProcess(int threadID, object parameter)
        {
            const string process = "notepad";

            while (!Charachter.Initialize(process)) System.Threading.Thread.Sleep(2500);
            Dispatcher.Invoke(new Action(() =>
            {
                Functions.LogWrite("Gotcha! PID: " + Charachter.Process.Id + ", base address: " + Charachter.Process.MainModule.BaseAddress + ".");
                TextBox tbPID = new TextBox { Text = "PID:\t\t" + Charachter.Process.Id };
                tbPID.SetResourceReference(StyleProperty, "CustomTextbox");
                expanderDebugInfo.Children.Add(tbPID);
                TextBox tbBaseaddress = new TextBox { Text = "Base address:\t" + Charachter.Process.MainModule.BaseAddress };
                tbBaseaddress.SetResourceReference(StyleProperty, "CustomTextbox");
                expanderDebugInfo.Children.Add(tbBaseaddress);
            }));
            _gameActive = true;

            while (Charachter.Initialize(process)) System.Threading.Thread.Sleep(2500);
            Dispatcher.Invoke(new Action(() => Functions.LogWrite("Process closed.")));
            _gameActive = false;

            CheckForProcess(0, null);
        }
        private void FillCharachterClass(int threadID, object parameter)
        {
            while (true)
            {
                if (_gameActive)
                {
                    try
                    {
                        Charachter.Fill();
                    }
                    catch
                    {
                    }
                }
                System.Threading.Thread.Sleep(50);
            }
        }
    }
}
